﻿#include "cquestion.h"

#include <QSqlQueryModel>


#include <sql/csqlmanager.h>



CQuestion::CQuestion()
{

}

int CQuestion::getQuestionAcount()
{
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    QSqlQuery query(db);

    query.exec("select * from " + QUES_TABLE_NAME + ";");
    QSqlQueryModel *model = new QSqlQueryModel();
    model->setQuery(query);
    int acount = model->rowCount();
    db.close();
    return acount;
}

int CQuestion::getAllQuestions(stQuestion *questionBuffer, int questionsSize)
{
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    QSqlQuery sql_query;
    sql_query.exec("select * from " + QUES_TABLE_NAME + ";");

    int index = 0;
    if(!sql_query.exec())
    {
        db.close();
        return -1;
    }
    else
    {
        while(sql_query.next())
        {
            if(index < questionsSize)
            {
                questionBuffer[index].protectionPosition    = sql_query.value(0).toString();
                questionBuffer[index].difficulty            = sql_query.value(1).toString();
                questionBuffer[index].innerNumber           = sql_query.value(2).toString();
                questionBuffer[index].fileMd5               = sql_query.value(3).toString();
                questionBuffer[index].flag                  = sql_query.value(4).toString();
                questionBuffer[index].knowledgePoints       = sql_query.value(5).toString();
                questionBuffer[index].fromWhere                  = sql_query.value(6).toString();
                index++;
            }
        }
    }

    db.close();
    return index;
}

/**
 * @brief 过滤筛选数据库中的信息
 * @param questionBuffer        buffer指针。如果要返回数据，数据往这里面写。
 * @param questionsSize         buffer的大小
 * @param colName               过滤的列名
 * @param condition             过滤条件
 * @param isDataBack            是否要返回过滤后的数据
 * @return                      返回满足条件的记录数
 */
int CQuestion::queryQuestionsTable(
        stQuestion *questionBuffer,
        int questionsSize,
        QString colName,
        QString condition,
        bool isDataBack)
{
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    QSqlQuery sql_query(db);

    QString sql = "select " + colName + " from " + QUES_TABLE_NAME + " where " + condition + ";";
    sql_query.exec(sql);
    int index = 0;
    if(!sql_query.exec())
    {
        db.close();
        return -1;
    }
    else
    {
        while(sql_query.next())
        {
            // 要求返回题目信息
            if (isDataBack)
            {
                if(index < questionsSize)
                {
                    questionBuffer[index].protectionPosition    = sql_query.value(0).toString();
                    questionBuffer[index].difficulty            = sql_query.value(1).toString();
                    questionBuffer[index].innerNumber           = sql_query.value(2).toString();
                    questionBuffer[index].fileMd5               = sql_query.value(3).toString();
                    questionBuffer[index].flag                  = sql_query.value(4).toString();
                    questionBuffer[index].knowledgePoints       = sql_query.value(5).toString();
                    questionBuffer[index].fromWhere                  = sql_query.value(6).toString();
                }
            }
            index++;

        }
    }

    db.close();
    return index;
}

bool CQuestion::isExistQuestion(QString colName, QString condition)
{
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    QSqlQuery sql_query(db);

    QString sql = "select " + colName + " from " + QUES_TABLE_NAME + " where " + condition + ";";
    sql_query.exec(sql);
    if(!sql_query.exec())
    {
        db.close();
        return false;
    }
    else
    {
        if(sql_query.next())
        {
            db.close();
            return true;
        }
    }
    db.close();
    return false;
}
